home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
byte0887.arc
/
SZPAK.LST
< prev
next >
Wrap
File List
|
1987-07-14
|
3KB
|
140 lines
--------------------------------------------------------------
SZPAK.LST The following 5 listings accompany the article
"Logic Grammars", by Stan Szpakowicz, in the August 1987 issue
of BYTE, page 185.
--------------------------------------------------------------
Listing 1:
«MDBO»
statements --> statement, [';'], statements.
statements --> [].
statement --> [skip].
statement --> [id(V)], [:=], expr.
statement --> [if], condition, [then], statements, [fi].
statement --> [while], condition, [do], statements, [od].
condition --> [not], relation.
condition --> relation.
relation --> expr, comp_op, expr.
comp_op --> ['='].
comp_op --> ['<'].
expression --> primary.
expression --> expression, arith_op, primary.
primary --> [id(V)].
primary --> [num(N)].
arith_op --> ['+'].
arith_op --> ['-'].
arith_op --> ['*'].
arith_op --> ['/'].
«MDNM»
[end listing 1]
Listing 2:
«MDBO»
/*1*/ statement(K, N) :-
token(id(V), K, L), token(:=, L, M), expr(M, N).
/*2*/ expr(K, L) :- primary(K, L).
/*3*/ expr(K, N) :- expr(K, L), arith_op(L, M), primary(M, N).
/*4*/ primary(K, L) :- token(id(V), K, L).
/*5*/ primary(K, L) :- token(num(V), K, L).
/*6*/ arith_op(K, L) :- token(+, K, L).
/*7*/ token(T, [T|Ts], Ts).
«MDNM»
[end listing 2]
«PG»
Listing 3:
«MDBO»
program(s(Stmt, Stmts)) -->
statement(Stmt), [';'],
statements(Stmts).
statements(s(Stmt, Stmts)) -->
statement(Stmt), [';'],
statements(Stmts).
statements(skip) --> [].
% a sequence of statements is represented as a nested term,
% for example s(Stmt1, s(Stmt2, s(Stmt3, skip))),
% where Stmt1, Stmt2, Stmt3 represent individual statements
statement(skip) --> [skip].
statement(let(V, E)) --> [id(V)], [:=], expr(E).
statement(if(C, Stmts)) -->
[if], condition(C), [then], statements(Stmts), [fi].
statement(while(C, Stmts)) -->
[while], condition(C), [do], statements(Stmts), [od].
condition(not(C)) --> [not], relation(C).
condition(C) --> relation(C).
relation(cond(Op, E1, E2)) --> expr(E1), comp_op(Op), expr(E2).
comp_op('=') --> ['='].
comp_op('<') --> ['<'].
«MDNM»
[end listing 3]
Listing 4:
«MDBO»
interm_code(s(Stmt, Stmts)) -->
interm_code(Stmt), interm_code(Stmts).
interm_code(skip) --> [].
interm_code(let(V, E)) -->
expr_interm_code(E), [store(V)].
interm_code(if(C, Stmts)) -->
{ newlabel(L) },
cond_interm_code(not(C)),
[jmp_cond(L)],
interm_code(Stmts),
[label(L)].
interm_code(while(C, Stmts)) -->
{ newlabel(L1) }, { newlabel(L2) },
[label(L1)],
cond_interm_code(not(C)),
[jmp_cond(L2)],
interm_code(Stmts),
[jmp(L1)], [label(L2)].
«MDNM»
[end listing 4]
Listing 5:
The source program:
«MDBO»
x := a; y := n; z := 1;
while not i < 1 do
if y / 2 * 2 < y then
z := z * x;
fi;
x := x * x;
y := y / 2;
od; #
«MDNM»
The resulting object code:
«MDBO»
load(a)
store(x)
load(n)
store(y)
loadc(1)
store(z)
label($lbl1)
loadc(1)
store($mem1)
load(i)
sub($mem1)
tst_neg
jmp_cond($lbl2)
etc.
«MDNM»
[end listing 5]